﻿using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Server.model
{
	/// <summary>
	/// 类user。
	/// </summary>
	[Serializable]
	public partial class user
	{
		public user()
		{
            this.id = -1;
            this.name = "";
            this.password = "";
            this.birthday = Convert.ToDateTime("1990-1-1");
            this.sexal = 2;
            this.bank_account = "";
            this.remark = "";
        }
		#region Model
		private int _id = -1;
		private string _name;
		private string _password;
        private DateTime _birthday;
		private int _sexal;
		private string _bank_account;
		private string _remark;
		/// <summary>
		/// auto_increment
		/// </summary>
		public int id
		{
			set{ _id=value;}
			get{return _id;}
		}
		/// <summary>
		/// 
		/// </summary>
		public string name
		{
			set{ _name=value;}
			get{return _name;}
		}
		/// <summary>
		/// 
		/// </summary>
		public string password
		{
			set{ _password=value;}
			get{return _password;}
		}
		/// <summary>
		/// 
		/// </summary>
		public DateTime birthday
		{
			set{ _birthday=value;}
			get{return _birthday;}
		}
		/// <summary>
		/// 
		/// </summary>
		public int sexal
		{
			set{ _sexal=value;}
			get{return _sexal;}
		}
		/// <summary>
		/// 
		/// </summary>
		public string bank_account
		{
			set{ _bank_account=value;}
			get{return _bank_account;}
		}
		/// <summary>
		/// 
		/// </summary>
		public string remark
		{
			set{ _remark=value;}
			get{return _remark;}
		}
		#endregion Model


		#region  Method

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public user(string name)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select `id`,`name`,`password`,`birthday`,`sexal`,`bank_account`,`remark` ");
			strSql.Append(" FROM `user` ");
			strSql.Append(" where `name`=@name ");
			MySqlParameter[] parameters = {
					new MySqlParameter("@name", MySqlDbType.VarChar, 50)};
			parameters[0].Value = name;

			DataSet ds=DbHelperMySQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["id"]!=null && ds.Tables[0].Rows[0]["id"].ToString()!="")
				{
					this.id=int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["name"]!=null)
				{
					this.name=ds.Tables[0].Rows[0]["name"].ToString();
				}
				if(ds.Tables[0].Rows[0]["password"]!=null)
				{
					this.password=ds.Tables[0].Rows[0]["password"].ToString();
				}
				if(ds.Tables[0].Rows[0]["birthday"]!=null && ds.Tables[0].Rows[0]["birthday"].ToString()!="")
				{
					this.birthday=DateTime.Parse(ds.Tables[0].Rows[0]["birthday"].ToString());
				}
				if(ds.Tables[0].Rows[0]["sexal"]!=null && ds.Tables[0].Rows[0]["sexal"].ToString()!="")
				{
					this.sexal=int.Parse(ds.Tables[0].Rows[0]["sexal"].ToString());
				}
				if(ds.Tables[0].Rows[0]["bank_account"]!=null)
				{
					this.bank_account=ds.Tables[0].Rows[0]["bank_account"].ToString();
				}
				if(ds.Tables[0].Rows[0]["remark"]!=null)
				{
					this.remark=ds.Tables[0].Rows[0]["remark"].ToString();
				}
			}
		}

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public static int GetMaxId()
		{

		return DbHelperMySQL.GetMaxID("`id`", "`user`"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public static bool Exists(string name)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from `user`");
			strSql.Append(" where `name`=@name ");

			MySqlParameter[] parameters = {
					new MySqlParameter("@name", MySqlDbType.VarChar, 50)};
			parameters[0].Value = name;

			return DbHelperMySQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public void Add()
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into `user` (");
			strSql.Append("`name`,`password`,`birthday`,`sexal`,`bank_account`,`remark`)");
			strSql.Append(" values (");
			strSql.Append("@name,@password,@birthday,@sexal,@bank_account,@remark)");
			MySqlParameter[] parameters = {
					new MySqlParameter("@name", MySqlDbType.VarChar,50),
					new MySqlParameter("@password", MySqlDbType.VarChar,50),
					new MySqlParameter("@birthday", MySqlDbType.Date),
					new MySqlParameter("@sexal", MySqlDbType.Int16,4),
					new MySqlParameter("@bank_account", MySqlDbType.VarChar,50),
					new MySqlParameter("@remark", MySqlDbType.VarChar,255)};
			parameters[0].Value = name;
			parameters[1].Value = password;
			parameters[2].Value = birthday;
			parameters[3].Value = sexal;
			parameters[4].Value = bank_account;
			parameters[5].Value = remark;

			DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update()
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update `user` set ");
			//strSql.Append("`name`=@name,");
			//strSql.Append("`password`=@password,");
			strSql.Append("`birthday`=@birthday,");
			strSql.Append("`sexal`=@sexal,");
			strSql.Append("`bank_account`=@bank_account,");
			strSql.Append("`remark`=@remark");
			strSql.Append(" where `id`=@id ");
			MySqlParameter[] parameters = {
					//new MySqlParameter("@name", MySqlDbType.VarChar,50),
					//new MySqlParameter("@password", MySqlDbType.VarChar,50),
					new MySqlParameter("@birthday", MySqlDbType.Date),
					new MySqlParameter("@sexal", MySqlDbType.Int16,4),
					new MySqlParameter("@bank_account", MySqlDbType.VarChar,50),
					new MySqlParameter("@remark", MySqlDbType.VarChar,255),
					new MySqlParameter("@id", MySqlDbType.Int32,11)};
			//parameters[0].Value = name;
			//parameters[0].Value = password;
			parameters[0].Value = birthday;
			parameters[1].Value = sexal;
			parameters[2].Value = bank_account;
			parameters[3].Value = remark;
			parameters[4].Value = id;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

        public bool UpdatePassword()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update `user` set ");
            strSql.Append("`password`=@password");
            strSql.Append(" where `id`=@id ");
            MySqlParameter[] parameters = {
                new MySqlParameter("@password", MySqlDbType.VarChar,50),
                new MySqlParameter("@id", MySqlDbType.Int32,11)};
            parameters[0].Value = password;
            parameters[1].Value = id;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static bool Delete(string name)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from `user` ");
			strSql.Append(" where `name`=@name ");
			MySqlParameter[] parameters = {
					new MySqlParameter("@name", MySqlDbType.VarChar, 50)};
			parameters[0].Value = name;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public void GetModel(int name)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select `id`,`name`,`password`,`birthday`,`sexal`,`bank_account`,`remark` ");
			strSql.Append(" FROM `user` ");
			strSql.Append(" where `name`=@name ");
			MySqlParameter[] parameters = {
					new MySqlParameter("@name", MySqlDbType.VarChar, 50)};
			parameters[0].Value = name;

			DataSet ds=DbHelperMySQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["id"]!=null && ds.Tables[0].Rows[0]["id"].ToString()!="")
				{
					this.id=int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["name"]!=null )
				{
					this.name=ds.Tables[0].Rows[0]["name"].ToString();
				}
				if(ds.Tables[0].Rows[0]["password"]!=null )
				{
					this.password=ds.Tables[0].Rows[0]["password"].ToString();
				}
				if(ds.Tables[0].Rows[0]["birthday"]!=null && ds.Tables[0].Rows[0]["birthday"].ToString()!="")
				{
					this.birthday=DateTime.Parse(ds.Tables[0].Rows[0]["birthday"].ToString());
				}
				if(ds.Tables[0].Rows[0]["sexal"]!=null && ds.Tables[0].Rows[0]["sexal"].ToString()!="")
				{
					this.sexal=int.Parse(ds.Tables[0].Rows[0]["sexal"].ToString());
				}
				if(ds.Tables[0].Rows[0]["bank_account"]!=null )
				{
					this.bank_account=ds.Tables[0].Rows[0]["bank_account"].ToString();
				}
				if(ds.Tables[0].Rows[0]["remark"]!=null )
				{
					this.remark=ds.Tables[0].Rows[0]["remark"].ToString();
				}
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select * ");
			strSql.Append(" FROM `user` ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperMySQL.Query(strSql.ToString());
		}

		#endregion  Method
	}
}

